package com.dianping.cat.consumer.problem.model.transform;

import com.dianping.cat.consumer.problem.model.IEntity;
import com.dianping.cat.consumer.problem.model.IVisitor;
import com.dianping.cat.consumer.problem.model.entity.Duration;
import com.dianping.cat.consumer.problem.model.entity.Entity;
import com.dianping.cat.consumer.problem.model.entity.Entry;
import com.dianping.cat.consumer.problem.model.entity.GraphTrend;
import com.dianping.cat.consumer.problem.model.entity.JavaThread;
import com.dianping.cat.consumer.problem.model.entity.Machine;
import com.dianping.cat.consumer.problem.model.entity.ProblemReport;
import com.dianping.cat.consumer.problem.model.entity.Segment;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;



























public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  53 */   public DefaultXmlBuilder() { this(false); }



/*  57 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  61 */     this.m_compact = compact;
/*  62 */     this.m_sb = sb;
/*  63 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  67 */     entity.accept(this.m_visitor);
/*  68 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  72 */     this.m_level--;

/*  74 */     indent();
/*  75 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  79 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  83 */     if (value == null) {
/*  84 */       return null;
    }

/*  87 */     String str = toString(value);
/*  88 */     int len = str.length();
/*  89 */     StringBuilder sb = new StringBuilder(len + 16);

/*  91 */     for (int i = 0; i < len; i++) {
/*  92 */       char ch = str.charAt(i);

/*  94 */       switch (ch) {
        case '<':
/*  96 */           sb.append("&lt;");
          break;
        case '>':
/*  99 */           sb.append("&gt;");
          break;
        case '&':
/* 102 */           sb.append("&amp;");
          break;
        case '"':
/* 105 */           if (!text) {
/* 106 */             sb.append("&quot;");
            break;
          }
        default:
/* 110 */           sb.append(ch);
          break;
      }

    }
/* 115 */     return sb.toString();
  }

  protected void indent() {
/* 119 */     if (!this.m_compact) {
/* 120 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 121 */         this.m_sb.append("   ");
      }
    }
  }


/* 127 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 131 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 135 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 139 */     indent();

/* 141 */     this.m_sb.append('<').append(name);

/* 143 */     int len = nameValues.length;

/* 145 */     for (int i = 0; i + 1 < len; i += 2) {
/* 146 */       Object attrName = nameValues[i];
/* 147 */       Object attrValue = nameValues[i + 1];

/* 149 */       if (attrValue != null) {
/* 150 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 154 */     if (dynamicAttributes != null) {
/* 155 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 156 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 160 */     if (text != null && closed) {
/* 161 */       this.m_sb.append('>');
/* 162 */       this.m_sb.append(escape(text, true));
/* 163 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 165 */       if (closed) {
/* 166 */         this.m_sb.append('/');
      } else {
/* 168 */         this.m_level++;
      }

/* 171 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 177 */     if (value instanceof String)
/* 178 */       return (String)value; 
/* 179 */     if (value instanceof Collection) {
/* 180 */       Collection<Object> list = (Collection)value;
/* 181 */       StringBuilder sb = new StringBuilder(32);
/* 182 */       boolean first = true;

/* 184 */       for (Object item : list) {
/* 185 */         if (first) {
/* 186 */           first = false;
        } else {
/* 188 */           sb.append(',');
        }

/* 191 */         if (item != null) {
/* 192 */           sb.append(item);
        }
      }

/* 196 */       return sb.toString();
/* 197 */     }  if (value.getClass().isArray()) {
/* 198 */       int len = Array.getLength(value);
/* 199 */       StringBuilder sb = new StringBuilder(32);
/* 200 */       boolean first = true;

/* 202 */       for (int i = 0; i < len; i++) {
/* 203 */         Object item = Array.get(value, i);

/* 205 */         if (first) {
/* 206 */           first = false;
        } else {
/* 208 */           sb.append(',');
        }

/* 211 */         if (item != null) {
/* 212 */           sb.append(item);
        }
      }

/* 216 */       return sb.toString();
    }

/* 219 */     return String.valueOf(value);
  }

  protected void tagWithText(String name, String text, Object... nameValues) {
/* 223 */     if (text == null) {
      return;
    }

/* 227 */     indent();

/* 229 */     this.m_sb.append('<').append(name);

/* 231 */     int len = nameValues.length;

/* 233 */     for (int i = 0; i + 1 < len; i += 2) {
/* 234 */       Object attrName = nameValues[i];
/* 235 */       Object attrValue = nameValues[i + 1];

/* 237 */       if (attrValue != null) {
/* 238 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 242 */     this.m_sb.append(">");
/* 243 */     this.m_sb.append(escape(text, true));
/* 244 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected void element(String name, String text, String defaultValue, boolean escape) {
/* 248 */     if (text == null || text.equals(defaultValue)) {
      return;
    }

/* 252 */     indent();

/* 254 */     this.m_sb.append('<').append(name).append(">");

/* 256 */     if (escape) {
/* 257 */       this.m_sb.append(escape(text, true));
    } else {
/* 259 */       this.m_sb.append("<![CDATA[").append(text).append("]]>");
    }

/* 262 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected String toString(Date date, String format) {
/* 266 */     if (date != null) {
/* 267 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 269 */     return null;
  }



  public void visitDuration(Duration duration) {
/* 275 */     startTag("duration", null, new Object[] { "value", Integer.valueOf(duration.getValue()), "count", Integer.valueOf(duration.getCount()) });

/* 277 */     if (!duration.getMessages().isEmpty()) {
/* 278 */       for (String message : duration.getMessages()) {
/* 279 */         tagWithText("message", message, new Object[0]);
      }
    }

/* 283 */     endTag("duration");
  }


  public void visitEntity(Entity entity) {
/* 288 */     startTag("entity", null, new Object[] { "id", entity.getId(), "type", entity.getType(), "status", entity.getStatus() });

/* 290 */     if (!entity.getThreads().isEmpty()) {
/* 291 */       for (JavaThread thread : entity.getThreads().values()) {
/* 292 */         thread.accept(this.m_visitor);
      }
    }

/* 296 */     if (!entity.getDurations().isEmpty()) {
/* 297 */       for (Duration duration : entity.getDurations().values()) {
/* 298 */         duration.accept(this.m_visitor);
      }
    }

/* 302 */     if (entity.getGraphTrend() != null) {
/* 303 */       entity.getGraphTrend().accept(this.m_visitor);
    }

/* 306 */     endTag("entity");
  }


  public void visitEntry(Entry entry) {
/* 311 */     startTag("entry", null, new Object[] { "type", entry.getType(), "status", entry.getStatus() });

/* 313 */     if (!entry.getDurations().isEmpty()) {
/* 314 */       for (Duration duration : entry.getDurations().values()) {
/* 315 */         duration.accept(this.m_visitor);
      }
    }

/* 319 */     if (!entry.getThreads().isEmpty()) {
/* 320 */       for (JavaThread thread : entry.getThreads().values()) {
/* 321 */         thread.accept(this.m_visitor);
      }
    }

/* 325 */     endTag("entry");
  }



/* 330 */   public void visitGraphTrend(GraphTrend graphTrend) { startTag("graph-trend", true, null, new Object[] { "duration", Integer.valueOf(graphTrend.getDuration()), "fails", graphTrend.getFails() }); }



  public void visitMachine(Machine machine) {
/* 335 */     startTag("machine", null, new Object[] { "ip", machine.getIp() });

/* 337 */     if (!machine.getEntries().isEmpty()) {
/* 338 */       for (Entry entry : machine.getEntries()) {
/* 339 */         entry.accept(this.m_visitor);
      }
    }

/* 343 */     if (!machine.getEntities().isEmpty()) {
/* 344 */       for (Entity entity : machine.getEntities().values()) {
/* 345 */         entity.accept(this.m_visitor);
      }
    }

/* 349 */     endTag("machine");
  }


  public void visitProblemReport(ProblemReport problemReport) {
/* 354 */     startTag("problem-report", null, new Object[] { "domain", problemReport.getDomain(), "startTime", toString(problemReport.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "endTime", toString(problemReport.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 356 */     if (!problemReport.getDomainNames().isEmpty()) {
/* 357 */       for (String domain : problemReport.getDomainNames()) {
/* 358 */         tagWithText("domain", domain, new Object[0]);
      }
    }

/* 362 */     if (!problemReport.getIps().isEmpty()) {
/* 363 */       for (String ip : problemReport.getIps()) {
/* 364 */         tagWithText("ip", ip, new Object[0]);
      }
    }

/* 368 */     if (!problemReport.getMachines().isEmpty()) {
/* 369 */       for (Machine machine : problemReport.getMachines().values()) {
/* 370 */         machine.accept(this.m_visitor);
      }
    }

/* 374 */     endTag("problem-report");
  }


  public void visitSegment(Segment segment) {
/* 379 */     startTag("segment", null, new Object[] { "id", segment.getId(), "count", Integer.valueOf(segment.getCount()) });

/* 381 */     if (!segment.getMessages().isEmpty()) {
/* 382 */       for (String message : segment.getMessages()) {
/* 383 */         tagWithText("message", message, new Object[0]);
      }
    }

/* 387 */     endTag("segment");
  }


  public void visitThread(JavaThread thread) {
/* 392 */     startTag("thread", null, new Object[] { "group-name", thread.getGroupName(), "name", thread.getName(), "id", thread.getId() });

/* 394 */     if (!thread.getSegments().isEmpty()) {
/* 395 */       for (Segment segment : thread.getSegments().values()) {
/* 396 */         segment.accept(this.m_visitor);
      }
    }

/* 400 */     endTag("thread");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/problem/model/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */